home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 July: Mac OS SDK / Dev.CD Jul 97 SDK1.toast / Development Kits (Disc 1) / QuickDraw GX / Programming Stuff / Sample Code / Graphics Samples / rotating rects ƒ / Rotating Rects.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-15  |  5.5 KB  |  175 lines  |  [TEXT/KAHL]

  1. /*
  2.     Rotating Rects
  3.     
  4.     This application will draw rectangles within the bounds the window.  The rectangles will be rotated, scaled, and 
  5.     colored with an HSV color.  All of the functions in this file are called by the graphics shell.
  6.     
  7.     NOTES:
  8.     • This file requires the following files to run correctly:
  9.         "graphics shell.c", "GraphicsDebugLibrary.c", and "TransformLibrary.c".
  10.         
  11.     • For the best printing results, print this file in "landscape".
  12.     
  13.     Change History:
  14.  
  15.        4/96    bob        Updated #includes to support changed GX Library names.
  16.                     Changed fixed to Fixed.
  17.                     Updated the note regarding the files needed to run.
  18.                     Updated the copyright date.
  19.  
  20.     ©1990 - 1996  Apple Computer, Inc.
  21.     All rights reserved.
  22. */
  23.  
  24.  
  25. #include <Events.h>
  26. #include <Windows.h>
  27.  
  28. #include "GraphicsLibraries.h"
  29. #include <GXEnvironment.h>
  30. #include "graphics shell.h"
  31.  
  32. #define f(a,b)    (((Fixed) (a) << 16) + (b))
  33. #define kNumberOfRectanglesDrawn     110
  34.  
  35. //
  36. //  Set up the title and size of the window 
  37. //
  38. Str255         gWindowTitle = "\p Rotating Rects (dither level = 4)";
  39. Rect         gWindowQDRect  = {50, 50, 330, 330};
  40.  
  41. //
  42. //    gGraphicsHeapSize sets the size of the graphics gxHeap created by calling the GXNewGraphicsClient routine
  43. //    in main () within graphics shell.c.  You can determine the amount of graphics gxHeap required by using GraphicsBug.
  44. //    With gGraphicsHeapSize set to 25k, I had 3 free blocks left in the graphics gxHeap. Sounds good to me.
  45. //
  46. long            gGraphicsHeapSize = 25;
  47.  
  48. gxShape         gRectangleShape;
  49. gxRectangle     gFixedWindowBounds;           /**  bounding box of the window in local coordinates  **/
  50. gxColor         gRectangleColor;
  51. Boolean            gChangeShapeFill;
  52. short            gTotalnumberOfRectanglesDrawn;
  53.  
  54.  
  55. /*------ DoInitialization ---------------------------------------------------------------------------------*/
  56.  
  57. void DoInitialization(aWindow)
  58. WindowPtr aWindow;
  59. {
  60.     gxViewPort        windowViewPortParent;
  61.     
  62.     gChangeShapeFill = true;
  63.     gTotalnumberOfRectanglesDrawn = 0;
  64.      
  65.     //
  66.     //    Get the viewPort that is attached to the window, and set the dither of this viewPort to 4.  Which
  67.     //    will mean that all objects that are drawn into window will be dithered at a dither level of 4.
  68.     //
  69.      windowViewPortParent = GXGetWindowViewPort(aWindow);
  70.     GXSetViewPortDither(windowViewPortParent, 4);
  71.  
  72.     //
  73.     //     Get the bounds of the window, and create a gxRectangle that will fill the window
  74.     //
  75.     GXGetShapeBounds(gWindowBoundsShape, 0L, &gFixedWindowBounds);
  76.     gRectangleShape = GXNewRectangle(&gFixedWindowBounds); 
  77.     
  78.     //
  79.      //     Set up an HSV gxColor space to run the rectangles through...  
  80.     //
  81.     gRectangleColor.space = gxHSVSpace;
  82.     gRectangleColor.profile = nil;
  83.     gRectangleColor.element.hsv.hue = 0x0000;
  84.     gRectangleColor.element.hsv.value = 0xFFFF;
  85.     gRectangleColor.element.hsv.saturation = 0xFFFF;
  86. }
  87.  
  88.  
  89. /*------ DoDraw ---------------------------------------------------------------------------------------*/
  90.  
  91. void DoDraw(aWindow)
  92. WindowPtr aWindow;
  93. {
  94.     gxRectangle     rectangleBoundsShape;
  95.     Fixed             x, y;
  96.  
  97.     if (gTotalnumberOfRectanglesDrawn ==  kNumberOfRectanglesDrawn) {
  98.         
  99.         //
  100.          //  Time to rebuild the gxRectangle...   Dispose of the "old" rectangle, and re-create a rectangle that will fill the window 
  101.         //
  102.         GXDisposeShape(gRectangleShape); 
  103.         gRectangleShape = GXNewRectangle(&gFixedWindowBounds); 
  104.     
  105.         //
  106.         //   Set the gxShapeFill type, alternate drawing the gxRectangle with a solid fill and a  gxClosedFrameFill
  107.         //
  108.         if (gChangeShapeFill == true) {
  109.              SetPort (aWindow);
  110.             EraseRect(&(aWindow)->portRect);
  111.             GXSetShapeFill (gRectangleShape, gxClosedFrameFill);
  112.                    gChangeShapeFill = false;
  113.             } 
  114.            else  gChangeShapeFill = true;
  115.                   
  116.         gTotalnumberOfRectanglesDrawn = 0;
  117.     }
  118.     
  119.     GXSetShapeColor(gRectangleShape, &gRectangleColor);
  120.     GXDrawShape(gRectangleShape);
  121.  
  122.     gRectangleColor.element.hsv.hue += 0x0300;
  123.     
  124.     //
  125.     //    Get the new bounds of the rectangle after it has been scaled by calling GXScaleShape. Each call to GXScaleShape
  126.     //    changes the bounding shape (box) of the rectangle. Determine the center of the bounding box, pass the center
  127.     //    to the GXRotateShape call. This will have the rectagnel rotated about the center.
  128.     //
  129.     GXGetShapeBounds(gRectangleShape, 0L, &rectangleBoundsShape);
  130.     x = rectangleBoundsShape.left + rectangleBoundsShape.right >> 1;
  131.     y = rectangleBoundsShape.top + rectangleBoundsShape.bottom >> 1;
  132.             
  133.     GXRotateShape(gRectangleShape, ff(2), x, y);
  134.     GXScaleShape(gRectangleShape, f(0, 0xF800), f(0, 0xF800), x, y);
  135.  
  136.     gTotalnumberOfRectanglesDrawn++;
  137. }
  138.  
  139.  
  140. /*------ DoDispose -------------------------------------------------------------------------------------*/
  141.  
  142. void DoDispose(aWindow)
  143. WindowPtr aWindow;
  144. {
  145.      //  
  146.     //    You should always dispose of your GX graphics objects before tossing your window. Why? It's generally good 
  147.     //    form and this approach guarantees that everything is disposed. If you had not disposed of everything, the
  148.     //    call to DisposeWindow should dispose of the objects. If you are running the debugging version of the 
  149.     //    SecretGraphics init with notices set, you will receive a notice that you had not disposed of everything. You
  150.     //    can turn notices on in this file by setting gDebugging = TRUE (above).
  151.     //  
  152.       GXDisposeShape(gRectangleShape); 
  153.     GXDisposeShape(gWindowBoundsShape);  
  154.     DisposeWindow(aWindow);
  155. }
  156.     
  157.  
  158.  
  159. /*------ DoClick ---------------------------------------------------------------------------------------*/
  160.  
  161. void DoClick( orgMouseLoc, theWindow )
  162. gxPoint        orgMouseLoc;
  163. WindowPtr     theWindow;
  164. {
  165. }
  166.  
  167.  
  168. /*------ DoIdle ----------------------------------------------------------------------------------------*/
  169.  
  170. void DoIdle(aWindow)
  171. WindowPtr aWindow;
  172. {
  173.     DoDraw(aWindow);
  174. }
  175.